package com.itany.mall.dao.impl;

import java.util.List;

import com.itany.mall.dao.ProductTypeDao;
import com.itany.mall.dao.mapper.ProductTypeRowMapper;
import com.itany.mall.entity.ProductType;
import com.itany.mall.factory.Component;
import com.itany.mall.jdbc.template.JDBCTemplate;

@Component
public class ProductTypeDaoImpl implements ProductTypeDao {

	@Override
	public List<ProductType> selectAll() {
		ProductTypeRowMapper mapper = new ProductTypeRowMapper();
		return new JDBCTemplate<Long, ProductType>().query(mapper.selectSql(), new Object[0],
				mapper);
	}

	@Override
	public ProductType selectByName(String name) {
		ProductTypeRowMapper mapper = new ProductTypeRowMapper();
		String sql = mapper.selectSql() + " where name = ? ";
		List<ProductType> list = new JDBCTemplate<Long, ProductType>().query(sql,
				new Object[] { name }, mapper);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public void insert(ProductType t) {
		String sql = " insert into t_product_type(name) values (?) ";
		new JDBCTemplate().update(sql, new Object[] { t.getName() });
	}

	@Override
	public ProductType selectById(long typeId) {
		ProductTypeRowMapper mapper = new ProductTypeRowMapper();
		String sql = mapper.selectSql() + " where id = ? ";
		List<ProductType> list = new JDBCTemplate<Long, ProductType>().query(sql,
				new Object[] { typeId }, mapper);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

}
